package com.taxinfo.view.subjectMatch;

import com.taxinfo.component.progress.TableProgressIndicator;
import com.taxinfo.component.table.ConditionTable;
import com.taxinfo.pojo.dto.SubMatchingDto;
import com.taxinfo.service.StandardSubService;
import com.taxinfo.service.SubMatchingService;
import com.taxinfo.utils.SpringUtil;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.control.Label;

import java.util.ArrayList;
import java.util.List;

public class SubjectMatchTable extends ConditionTable<SubMatchingDto> {

    private static final String MENU_CODE = "subjectMath";

    private final SubMatchingService subMatchingService = (SubMatchingService) SpringUtil.getBean("SubMatchingServiceImpl");
    private final ObservableList<SubMatchingDto> items = getItems();


    public SubjectMatchTable(List<SubMatchingDto> items) {
        super(MENU_CODE, items, SubMatchingDto.class);
        this.items.addAll(items);
        showAllInfos();
    }


    /**
     * 显示所有会员信息
     */
    public void showAllInfos() {
        clearData();
        setPlaceholder(new TableProgressIndicator());

        Task<List<SubMatchingDto>> task = new Task<List<SubMatchingDto>>() {
            @Override
            protected List<SubMatchingDto> call() {
                List<SubMatchingDto> beans = subMatchingService.selectAllData("");
                if (beans != null){
                    return beans;
                }else{
                    return new ArrayList<>(0);
                }
            }
        };
        new Thread(task).start();
        task.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.size() != 0){
                items.addAll(newValue);
            } else{
                setPlaceholder(new Label("没有数据"));
            }
        });
    }

    /**
     * 添加数据
     * @param dto
     */
    public void addBean(SubMatchingDto dto) {
        items.add(dto);
    }
}